/**
 * 
 */
package com.zhiteng.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.zhiteng.service.SmartCampusApiService;
import com.zhiteng.utils.common.HttpUtil;
import com.zhiteng.utils.tencent.campus.DecodeUtil;
import com.zhiteng.utils.tencent.campus.ParamUtil;
import com.zhiteng.utils.tencent.campus.SignUtil;

/**  
* <p>Title: SmartCampusApiServiceImpl.java</p>  
* <p>Description: 腾讯智慧校园API Service</p>  
* @author dragon  
* @date 2018年12月16日  
* @version 1.0  
*/
@Service
public class SmartCampusApiServiceImpl implements SmartCampusApiService {
	
	@Value("${devCode}")
	private String devCode;
	@Value("${devType}")
	private String devType;
	@Value("${objType}")
	private String objType;
	@Value("${officeid}")
	private String officeid;
	@Value("${openAppID}")
	private String openAppID;
	
	@Value("${sendMsgURL}")
	private String sendMsgURL;
	@Value("${keyForMsg}")
	private String keyForMsg;
	
	/**
	 * 发消息
	 * objectid:学校id
	 * 注意：Map的变量名必须是data(腾讯api要求)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> sendMsg(String objectid,Map<String, String> data) {
		long timestamp = System.currentTimeMillis();
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("timestamp", timestamp);
		paramMap.put("openAppID", openAppID);
		paramMap.put("objType", objType);
		paramMap.put("data", data);
		paramMap.put("objectid", objectid);
		String sign = SignUtil.getSign(paramMap,keyForMsg);
		paramMap.put("sign", sign);
		
		String result = HttpUtil.doPost(sendMsgURL, ParamUtil.getParamMap(paramMap));
		//	map格式的json字符串转为map
		Map<String,Object> resultMap = (Map<String,Object>) JSONObject.parse(DecodeUtil.decodeUnicode(result));	
		return resultMap;
	}

}
